/*
 * Copyright (c) 2018, apexes.net. All rights reserved.
 *
 *         http://www.apexes.net
 *
 */
package net.apexes.demo.codegen;

import com.querydsl.sql.DerbyTemplates;
import com.querydsl.sql.SQLTemplates;
import net.apexes.codegen.core.CodegenExporter;
import net.apexes.codegen.core.JavabeanSerializer;
import net.apexes.codegen.core.OrmliteColumnMetadataSerializer;
import net.apexes.commons.lang.Resources;
import net.apexes.commons.querydsl.support.QuerydslEnumeSupport;
import net.apexes.demo.codegen.entity.base.EntityBase;
import net.apexes.demo.codegen.enume.support.DemoEnumeSupport;
import net.apexes.demo.codegen.ormlite.column.base.QEntityBase;
import net.apexes.demo.codegen.util.DemoDatabases;

import java.io.File;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author <a href="mailto:hedyn@foxmail.com">HeDYn</a>
 *
 */
public class DemoOrmliteColumnCodegen {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        try (Connection conn = DemoDatabases.createConnection()) {
            List<String> tables = findTables(conn);
            codegen(DerbyTemplates.builder().build(), conn, tables);
        }
        System.out.println("Codegen Over!");
    }

    private static final String ENTITY_PACKAGE = "net.apexes.demo.codegen.ormlite.entity";
    private static final String QVAR_PACKAGE = "net.apexes.demo.codegen.ormlite.column";
    private static final String TARGET = "apexes-codegen-demo-query/src/main/java";

    private static List<String> findTables(Connection conn) throws Exception {
        List<String> tables = new ArrayList<>();
        DatabaseMetaData md = conn.getMetaData();
        try (ResultSet rs = md.getTables(null, "PUBLIC", null, new String[]{"TABLE"})) {
            while (rs.next()) {
                String tableName = rs.getString("TABLE_NAME");
                tables.add(tableName);
            }
            return tables;
        }
    }

    private static void codegen(SQLTemplates templates, Connection conn, List<String> tables) throws Exception {
        String projectPath = Resources.getProjectPath(DemoOrmliteColumnCodegen.class);
        File file = new File(projectPath);
        while (!"apexes-codegen".equals(file.getName())) {
            file = file.getParentFile();
        }

        Path targetFolder = file.toPath().resolve(TARGET);

        CodegenExporter exporter = new CodegenExporter();
        exporter.setJavabeanSupertype(EntityBase.class);
        exporter.setMetadataSupertype(QEntityBase.class);
        exporter.setConfiguration(DemoEnumeSupport.getInstance().register(QuerydslEnumeSupport.simpleConfiguration(templates), tables));
        exporter.setSchemaPattern("PUBLIC");
        exporter.setInnerClassesForKeys(true);
        exporter.setJavabeanSerializer(new JavabeanSerializer(""));
        exporter.setMetadataSerializerClass(OrmliteColumnMetadataSerializer.class);
        exporter.setJavabeanTargetFolder(targetFolder);
        exporter.setMetadataTargetFolder(targetFolder);
        exporter.setJavabeanPackageName(ENTITY_PACKAGE);
        exporter.setMetadataPackageName(QVAR_PACKAGE);
        exporter.setJdbcTypeConverter(jdbcType -> {
            if (java.sql.Types.CHAR == jdbcType) {
                return Types.VARCHAR;
            }
            return jdbcType;
        });
        exporter.export(conn.getMetaData());
    }

}
